<!DOCTYPE html>
<html>
  <head>
    <title>打砖块游戏</title>
    <style>
      canvas {
        border: 1px solid black;
      }
    </style>
  </head>
  <body>
    <canvas id="canvas" width="1000" height="400"></canvas>

    <script>
      const canvas = document.getElementById("canvas");
      const context = canvas.getContext("2d");

      const ballRadius = 10;
      let x = canvas.width / 2;
      let y = canvas.height - 30;
      let dx = 3;
      let dy = -3;

      const paddleHeight = 10;
      const paddleWidth = 275;
      let paddleX = (canvas.width - paddleWidth) / 2;

      let rightPressed = false;
      let leftPressed = false;

      const brickRowCount = 3;
      const brickColumnCount = 5;
      const brickWidth = 200;
      const brickHeight = 30;
      const brickPadding = 10;
      const brickOffsetTop = 30;
      const brickOffsetLeft = 30;
      const bricks = [];

      for (let c = 0; c < brickColumnCount; c++) {
        bricks[c] = [];
        for (let r = 0; r < brickRowCount; r++) {
          bricks[c][r] = { x: 0, y: 0, status: 1 };
        }
      }

      let score = 0;
      let lives = 3;

      function drawBall() {
        context.beginPath();
        context.arc(x, y, ballRadius, 0, Math.PI * 2);
        context.fillStyle = "#0095DD";
        context.fill();
        context.closePath();
      }

      function drawPaddle() {
        context.beginPath();
        context.rect(paddleX, canvas.height - paddleHeight, paddleWidth, paddleHeight);
        context.fillStyle = "#0095DD";
        context.fill();
        context.closePath();
      }

      function drawBricks() {
        for (let c = 0; c < brickColumnCount; c++) {
          for (let r = 0; r < brickRowCount; r++) {
            if (bricks[c][r].status == 1) {
              const brickX = c * (brickWidth + brickPadding) + brickOffsetLeft;
              const brickY = r * (brickHeight + brickPadding) + brickOffsetTop;
              bricks[c][r].x = brickX;
              bricks[c][r].y = brickY;
              context.beginPath();
              context.rect(brickX, brickY, brickWidth, brickHeight);
              context.fillStyle = "#0095DD";
              context.fill();
              context.closePath();
            }
          }
        }
      }

      function collisionDetection() {
        for (let c = 0; c < brickColumnCount; c++) {
          for (let r = 0; r < brickRowCount; r++) {
            const b = bricks[c][r];
            if (b.status == 1) {
              if (x > b.x && x < b.x + brickWidth && y > b.y && y < b.y + brickHeight) {
                dy = -dy;
                b.status = 0;
                score++;
                if (score == brickRowCount * brickColumnCount) {
                  alert("YOU WIN, CONGRATULATIONS!");
                  document.location.reload(); // 页面重新加载
                }
              }
            }
          }
        }
      }

      function drawScore() {
        context.font = "16px Arial";
        context.fillStyle = "#0095DD";
        context.fillText("Score: " + score, 8, 20);
      }

      function drawLives() {
        context.font = "16px Arial";
        context.fillStyle = "#0095DD";
        context.fillText("Lives: " + lives, canvas.width - 65, 20);
      }

      function draw() {
        context.clearRect(0, 0, canvas.width, canvas.height);
        drawBricks();
        drawBall();
        drawPaddle();
        drawScore();
        drawLives();
        collisionDetection();

        if (x + dx > canvas.width - ballRadius || x + dx < ballRadius) {
          dx = -dx;
        }

        if (y + dy < ballRadius) {
          dy = -dy;
        } else if (y + dy > canvas.height - ballRadius) {
          if (x > paddleX && x < paddleX + paddleWidth) {
            dy = -dy;
          } else {
            lives--;
            if (!lives) {
              alert("GAME OVER");
              document.location.reload(); // 页面重新加载
            } else {
              x = canvas.width / 2;
              y = canvas.height - 30;
              dx = 3;
              dy = -3;
              paddleX = (canvas.width - paddleWidth) / 2;
            }
          }
        }

        if (rightPressed && paddleX < canvas.width - paddleWidth) {
          paddleX += 7;
        } else if (leftPressed && paddleX > 0) {
          paddleX -= 7;
        }

        x += dx;
        y += dy;

        requestAnimationFrame(draw);
      }

      document.addEventListener("keydown", keyDownHandler);
      document.addEventListener("keyup", keyUpHandler);

      function keyDownHandler(e) {
        if (e.keyCode == 39) {
          rightPressed = true;
        } else if (e.keyCode == 37) {
          leftPressed = true;
        }
      }

      function keyUpHandler(e) {
        if (e.keyCode == 39) {
          rightPressed = false;
        } else if (e.keyCode == 37) {
          leftPressed = false;
        }
      }

      draw();
    </script>
  </body>
</html>
<div style="border: 2px solid orange; background-color: #ffe6cc; padding: 10px;">
  <h2 style="color: #ff6600">游戏规则：</h2>
  <ul>
    <li>使用左右箭头键来移动挡板，确保小球不会碰到底部。</li>
    <li>当小球撞击砖块时，砖块将被消除，您的得分也会相应增加。</li>
    <li>消除全部砖块后获得胜利，失去三条生命则游戏结束。</li>
  </ul>
</div>